﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity.ServiceLocatorAdapter;
using KandaAlpha.Application.Interfaces;
using KandaAlpha.UI.Mvc.Controllers;
using KandaAlpha.Application.Impl;
using KandaAlpha.Domain.Repository;
using KandaAlpha.Domain.Model.Entities;
using KandaAlpha.Infrastructure.Repository.db4o;

namespace KandaAlpha.UI.Mvc.Infrastructure.Ioc
{
    /// <summary>
    /// Sets up and configures inversion of control
    /// </summary>
    public static class IocBootstrapper
    {
        public static void Initialise()
        {

            // create new unity container
            var container = new UnityContainer();


            // injection for kanda.application.interfaces 
            container.RegisterType<IApplicationService, ApplicationService>();
            container.RegisterType<IUnitOfWorkFactory, UnitOfWorkFactory>();
            container.RegisterType<IUnitOfWork, UnitOfWork>();
            container.RegisterType<ICustomerService, CustomerService>();
            container.RegisterType<IClassifierService, ClassifierService>();

            // injecttion for  kanda.domain.repository 
            container.RegisterType<IRepositoryContext, RepositoryContext>();
            container.RegisterType<ICustomerRepository, CustomerRepository>();
            container.RegisterType<IClassifierRepository, ClassifierRepository>();

            // injection for kanda.infrastructure.repository.db40
            var injectionConstructor = new InjectionConstructor(ConfigurationManager.AppSettings["db4o.path"], Convert.ToInt32(ConfigurationManager.AppSettings["db4o.serverPort"]));
            container.RegisterType<IObjectContainerManager, ObjectContainerManager>(injectionConstructor);
                
            container.RegisterType(typeof(IGenericRepository<>), typeof(GenericRepository<>));

            // set unity as the service locator provider
            var serviceLocator = new UnityServiceLocator(container);
            ServiceLocator.SetLocatorProvider(() => serviceLocator);
        }

    }
}